package com.github.niefy.modules.wx.scheduling;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.niefy.common.utils.ChangDuCallService;
import com.github.niefy.modules.wx.entity.ChangduUserInfo;
import com.github.niefy.modules.wx.entity.ChangduUserRecharge;
import com.github.niefy.modules.wx.service.ChangduUserRechargeService;
import com.github.niefy.modules.wx.service.impl.WeiBoServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Slf4j
public class ChangduUserReChargeJob {
    @Autowired
    private ChangDuCallService changDuCallService;
    @Autowired
    private ChangduUserRechargeService changduUserRechargeService;
    @Autowired
    private WeiBoServiceImpl weiBoService;

    // 每30分钟执行一次
    // @Scheduled(fixedRate = 30 * 60 * 1000)
    // 你可以将其设置为60000毫秒（即一分钟
    @Scheduled(fixedRate = 60000)
    @Transactional(rollbackFor = Exception.class)
    public void changdu_getUserReChargeV1() {
        List<ChangduUserRecharge> callList = changDuCallService.getUserReChargeV1();

        List<Long> tradeNoList = callList.stream().map(ChangduUserRecharge::getTradeNo).collect(Collectors.toList());
        List<ChangduUserRecharge> dbList = changduUserRechargeService.listByIdService(tradeNoList);
        Set<Long> dbSet = dbList.stream().map(ChangduUserRecharge::getTradeNo).collect(Collectors.toSet());

        List<ChangduUserRecharge> userRechargeList = callList
                .stream()
                .filter(changduUserRecharge -> !dbSet.contains(changduUserRecharge.getTradeNo()))
                .collect(Collectors.toList());

        if (ObjectUtils.isEmpty(userRechargeList)) {
            log.info("ChangduUserReChargeJob changdu_getUserReChargeV1 此次没有拉取到新增的数据");
            return;
        }

        Map<String, ChangduUserInfo> userListV1ReturnOpenIdMap = changDuCallService.getUserListV1ReturnOpenIdMap();
        for (ChangduUserRecharge userRecharge : userRechargeList) {
            String openId = userRecharge.getOpenId();

            if (userListV1ReturnOpenIdMap.containsKey(openId)) {
                ChangduUserInfo changduUserInfo = userListV1ReturnOpenIdMap.get(openId);
                userRecharge.setIp(changduUserInfo.getIp());
                userRecharge.setUserAgent(changduUserInfo.getUserAgent());
            }
        }


        for (ChangduUserRecharge temp : userRechargeList) {
            try {
                changduUserRechargeService.saveService(Lists.newArrayList(temp));
                weiBoService.wb_JianCeLianJie_CallWeiBo_fu_fei(Lists.newArrayList(temp));
            } catch (Exception e) {
                log.error("ChangduUserReChargeJob changdu_getUserReChargeV1 saveService wb_JianCeLianJie_CallWeiBo_fu_fei 失败了", e);
            }
        }
    }

}
